package org.zebra.logging.processor;

import io.sentry.SentryLevel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.event.Level;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

@Configuration
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String enabled = environment.getProperty("zebra.log.sentry.enabled");
        if (StringUtils.isNotBlank(enabled) && enabled.equals("true")) {
            String dsn = environment.getProperty("zebra.log.sentry.dsn");
            String active = environment.getProperty("spring.profiles.active");
            if (StringUtils.isBlank(active)) {
                active = "default";
            }
            String diagnosticLevel = environment.getProperty("zebra.log.sentry.diagnosticLevel");
            String minimumEventLevel = environment.getProperty("zebra.log.sentry.minimumEventLevel");
            String minimumBreadcrumbLevel = environment.getProperty("zebra.log.sentry.minimumBreadcrumbLevel");
            // 动态添加属性值
            Properties properties = new Properties();
            properties.setProperty("sentry.dsn", dsn);
            properties.setProperty("sentry.environment", active);
            properties.setProperty("sentry.diagnosticLevel", dealDiagnosticLevel(active, diagnosticLevel));
            properties.setProperty(
                    "sentry.logging.minimumEventLevel", dealMinimumEventLevel(active, minimumEventLevel));
            properties.setProperty(
                    "sentry.logging.minimumBreadcrumbLevel",
                    dealMinimumBreadcrumbLevel(active, minimumBreadcrumbLevel));

            MutablePropertySources propertySources = environment.getPropertySources();
            propertySources.addLast(new PropertiesPropertySource("customProperties", properties));
        }
    }

    private String dealDiagnosticLevel(String active, String diagnosticLevel) {
        if (StringUtils.isNotBlank(diagnosticLevel)) {
            return diagnosticLevel;
        } else {
            if ("prod".contains(active)) {
                return SentryLevel.ERROR.toString();
            } else {
                return SentryLevel.DEBUG.toString();
            }
        }
    }

    private String dealMinimumEventLevel(String active, String minimumEventLevel) {
        if (StringUtils.isNotBlank(minimumEventLevel)) {
            return minimumEventLevel;
        } else {
            if ("prod".contains(active)) {
                return Level.ERROR.toString();
            } else {
                return Level.DEBUG.toString();
            }
        }
    }

    private String dealMinimumBreadcrumbLevel(String active, String minimumBreadcrumbLevel) {
        if (StringUtils.isNotBlank(minimumBreadcrumbLevel)) {
            return minimumBreadcrumbLevel;
        } else {
            if ("prod".contains(active)) {
                return Level.ERROR.toString();
            } else {
                return Level.DEBUG.toString();
            }
        }
    }
}
